//
//  AppDelegate+Pay.m
//  乡易秀商户端
//
//  Created by delicious on 17/9/20.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "AppDelegate+Pay.h"
// 如果需要使用idfa功能所需要引入的头文件（可选）
#import <AdSupport/AdSupport.h>
@implementation AppDelegate (Pay)
-(BOOL)onOpenAliPayForUrl:(NSURL *)url application:(UIApplication *)application Callback:(CompletionBlock)completionBlock
{

    if ([url.host isEqualToString:@"safepay"]) {
        // 支付跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            if(completionBlock)
            {
                completionBlock(resultDic);
            }
        }];
        
        // 授权跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result >>>>= %@",resultDic);//  resultStatus = "9000",成功
            
            if(completionBlock)
            {
                completionBlock(resultDic);
            }
            
            // 解析 auth code
            NSString *result = resultDic[@"result"];
            NSString *authCode = nil;
            if (result.length>0) {
                NSArray *resultArr = [result componentsSeparatedByString:@"&"];
                for (NSString *subResult in resultArr) {
                    if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
                        authCode = [subResult substringFromIndex:10];
                        break;
                    }
                }
            }
            NSLog(@"授权结果 authCode = %@", authCode?:@"");
        }];
        
       
    }else
    {
        return [WXApi handleOpenURL:url delegate:self];
    }
    
    return YES;
}




//微信的支付结果
- (void)onResp:(BaseResp *)resp
{
    NSString * strMsg = [NSString stringWithFormat:@"errorCode: %d",resp.errCode];
    NSLog(@"strMsg: %@",strMsg);
    
    NSString * errStr       = [NSString stringWithFormat:@"errStr: %@",resp.errStr];
    NSLog(@"errStr: %@",errStr);
    
    
    NSString * strTitle;
    //判断是微信消息的回调 --> 是支付回调回来的还是消息回调回来的.
    if ([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        strTitle = [NSString stringWithFormat:@"发送媒体消息的结果"];
    }
    
    NSString * wxPayResult;
    //判断是否是微信支付回调 (注意是PayResp 而不是PayReq)
    
    if ([resp isKindOfClass:[PayResp class]])
    {
        //支付返回的结果, 实际支付结果需要去微信服务器端查询
        strTitle = [NSString stringWithFormat:@"支付结果"];
        switch (resp.errCode)
        {
            case WXSuccess:
            {
                strMsg = @"支付结果:";
                NSLog(@"支付成功: %d",resp.errCode);
                wxPayResult = @"success";
                break;
            }
            case WXErrCodeUserCancel:
            {
                strMsg = @"用户取消了支付";
                NSLog(@"用户取消支付: %d",resp.errCode);
                wxPayResult = @"cancel";
                break;
            }
            default:
            {
                strMsg = [NSString stringWithFormat:@"支付失败! code: %d  errorStr: %@",resp.errCode,resp.errStr];
                NSLog(@":支付失败: code: %d str: %@",resp.errCode,resp.errStr);
                wxPayResult = @"faile";
                break;
            }
        }
        //发出通知 从微信回调回来之后,发一个通知,让请求支付的页面接收消息,并且展示出来,或者进行一些自定义的展示或者跳转
        NSNotification * notification = [NSNotification notificationWithName:noteWXPayResult object:wxPayResult];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    }
}

//{
//    result = "{
//    "alipay_trade_app_pay_response":{
//        "code":"10000",
//        "msg":"Success",
//        "app_id":"2017091508738871",
//        "auth_app_id":"2017091508738871",
//        "charset":"UTF-8",
//        "timestamp":"2017-09-20 11:25:45",
//        "total_amount":"0.10",
//        "trade_no":"2017092021001004170522561357",
//        "seller_id":"2088721449924077",
//        "out_trade_no":"jy00000000427719f01336f2ebbe0489"
//    },
//    "sign":"aoc4GaT3KYADDOZvmxtdsoON/sZVXph/p1cjwvzJRr6/+XBMpoNMZabomLprFYLKvlbwlamYl/9562HnmiZLKOro0Vqik+KvLpFJplAoJfwodrXlmD5xTkV3uJvTbUTmsc8DgNTLY7e4BH/eBTjfKK1Tj7OEH4tSMue7pMuV3Ig=",
//    "sign_type":"RSA"
//}",
//
//    memo = ""
//}

-(void)JPushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
     NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    
//    [JPUSHService setupWithOption:launchOptions appKey:KJGKey
//                          channel:@"App Store"
//                 apsForProduction:isProduction
    
//            advertisingIdentifier:advertisingId];
    [MCPushManag setupWithOption:launchOptions appKey:KJGKey channel:@"App Store" apsForProduction:isProduction advertisingIdentifier:advertisingId];
    
}



@end
